﻿/*
* 表格控件
*/
(function ($) {
    var params,
    $grid,
    loadData = function (options) {
        $.ajax({
            type: options.type,
            datatype: options.datatype,
            url: options.url,
            contentType: options.contentType,
            data: JSON.stringify(options.query),
            success: success
        });
    },
    success = function (result) {
        if (params.beforeSuccess(result) == false) {
            return;
        }

        var rsp = result.d;
        $grid.find('tbody').html(rsp.gridHtml);
        setPagerHtml(params.pager, rsp.pageSize, rsp.pageIndex, rsp.recordCound);
        if (rsp.msg != '') {
            alert(rsp.msg);
        }

        //利用代理
        jQuery.BangleGridHelper.pagerGo = pagerGo;

        params.afterSuccess(result);
    },
    setPagerHtml = function (obj, pageSize, pageIndex, pageCount) {
        var _sPagerHtml = "";
        var _iSize = (parseInt((pageCount - 1) / pageSize) + 1);

        _sPagerHtml += '<div style="width: 60%; float: left;" align="left">';

        if (pageIndex == 1) {
            _sPagerHtml += '<a style="margin-right: 5px;" disabled="disabled" >首页</a>';
            _sPagerHtml += '<a style="margin-right: 5px;" disabled="disabled" )">上一页</a>';
        }
        else {

            _sPagerHtml += '<a style="margin-right: 5px;" href="javascript:$.BangleGridHelper.pagerGo(1)">首页</a>';
            _sPagerHtml += '<a style="margin-right: 5px;" href="javascript:$.BangleGridHelper.pagerGo(' + (pageIndex - 1) + ')">上一页</a>';
        }

        for (var i = 1; i <= _iSize ; i++) {
            if (i == pageIndex) {
                _sPagerHtml += '<span style="color: red; font-weight: bold; margin-right: 5px;">' + i + '</span>';
            }
            else {
                _sPagerHtml += '<a style="margin-right: 5px;" href="javascript:$.BangleGridHelper.pagerGo(' + i + ')">' + i + '</a>';
            }
        }
        if (pageIndex == _iSize) {
            _sPagerHtml += '<a style="margin-right: 5px;" disabled="disabled" >下一页</a>';
            _sPagerHtml += '<a style="margin-right: 5px;" disabled="disabled" )">尾页</a>';
        }
        else {
            _sPagerHtml += '<a style="margin-right: 5px;" href="javascript:$.BangleGridHelper.pagerGo(' + (pageIndex + 1) + ')">下一页</a>';
            _sPagerHtml += '<a style="margin-right: 5px;" href="javascript:$.BangleGridHelper.pagerGo(' + (_iSize) + ')">尾页</a>';
        }

        _sPagerHtml += '</div>';
        _sPagerHtml += '<div id="pager-right" style="width: 40%; float: left;">';
        _sPagerHtml += '记录总数：<font color="blue"><b>' + pageCount + '</b></font>';
        _sPagerHtml += '</div>';

        $(obj).html(_sPagerHtml);
    },
    pagerGo = function (index) {
        if ($.fn.BangleGrid.defaults.beforePager(index) == false) {
            return;
        }
        $.fn.BangleGrid.defaults.query.pageIndex = index;
        loadData($.fn.BangleGrid.defaults);

        $.fn.BangleGrid.defaults.afterPager(index);
    };

    $.fn.BangleGrid = function (options) {
        $grid = $(this);
        options.query = $.extend($.fn.BangleGrid.defaults.query, options.query);
        params = $.extend($.fn.BangleGrid.defaults, options);
        $.fn.BangleGrid.defaults = params;
        loadData(params);

        $grid.find('th.th-order').unbind("click");
        $grid.find('th.th-order').click(function () {
            $grid.find('th.th-order b').remove();

            var _sOrderField = $(this).attr('data-field');
            if ($(this).hasClass('th-order-desc')) {
                $(this).removeClass('th-order-desc');
                $(this).append('<b>▲</b>');
            } else {
                _sOrderField += " desc";
                $(this).addClass('th-order-desc');
                $(this).append('<b>▼</b>');
            }
            $.fn.BangleGrid.defaults.query.OrderField = _sOrderField;
            pagerGo(1);
        });
    };

    $.fn.BangleGrid.refresh = function () {
        pagerGo(params.query.pageIndex);
    }

    $.fn.BangleGrid.defaults = {
        pager: '',
        url: "",
        type: "post",
        datatype: "json",
        contentType: "application/json",
        query: {
            pageSize: 30,
            pageIndex: 1,
            OrderField: '',
            QueryString: ''
        },
        beforeSuccess: function (result) {
            return true;
        },
        afterSuccess: function (result) {

        },
        beforePager: function (index) {

        },
        afterPager: function (index) {

        }
    }
})(jQuery);

jQuery.BangleGridHelper = {
    pagerGo: function (index) {

    }
}
